package com.example.dormitory_manager.controller;


import com.example.dormitory_manager.entity.Building;
import com.example.dormitory_manager.entity.Student;
import com.example.dormitory_manager.form.SearchForm;
import com.example.dormitory_manager.form.StudentForm;
import com.example.dormitory_manager.service.BuildingService;
import com.example.dormitory_manager.utils.ResultVOUtil;
import com.example.dormitory_manager.vo.PageVo;
import com.example.dormitory_manager.vo.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-10-02
 */
@RestController
@RequestMapping("/building")
public class BuildingController {
    @Autowired
    private BuildingService buildingService;

    @PostMapping("/save")
    public ResultVo save(@RequestBody Building building){
        boolean save =this.buildingService.save(building);
        if(!save) return ResultVOUtil.fail();
        return  ResultVOUtil.success(null);
    }

    @GetMapping("/list/{page}/{size}")
    public  ResultVo list(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        PageVo pageVo =this.buildingService.list(page,size);
        return  ResultVOUtil.success(pageVo);
    }

    @GetMapping("/search")
    public ResultVo search(SearchForm searchForm){
        return  ResultVOUtil.success(this.buildingService.search(searchForm));
    }

    @GetMapping("/findById/{id}")
    public ResultVo findById(@PathVariable("id") Integer id) {
        return ResultVOUtil.success(this.buildingService.getById(id));
    }

    @PutMapping("/update")
    public ResultVo update(@RequestBody Building building) {
        Boolean update=this.buildingService.updateById(building);
        if(!update) return  ResultVOUtil.fail();
        return  ResultVOUtil.success(null);
    }

    @DeleteMapping("/delete/{id}")
    public ResultVo deleteById(@PathVariable("id") Integer id){
        boolean delete=this.buildingService.deleteById(id);
        if(!delete) return  ResultVOUtil.fail();
        return ResultVOUtil.success(null);
    }
}

